package sz.szsmk.citizencard.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chenenyu.router.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;

import javax.inject.Inject;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.request.AccumPointReq;
import sz.szsmk.citizencard.model.network.bean.response.AccumPointRes;
import sz.szsmk.citizencard.model.network.bean.response.SignInRes;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.ISignInMvpView;
import sz.szsmk.citizencard.mvp.presenter.SignInPresenter;
import sz.szsmk.citizencard.ui.dzsmkavtivity.AppBaseActivity;
import sz.szsmk.citizencard.ui.popwindow.CalendarPopWindow;
import sz.szsmk.citizencard.util.ButtonUtils;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * author : hyt
 * date : 2019/8/16
 * description : 我的积分
 */
@Route("SignIn")
public class SignInActivity extends AppBaseActivity implements ISignInMvpView, View.OnClickListener {

    @Inject
    SignInPresenter presenter;

    RecyclerView recycle_list;

    private BaseQuickAdapter adapter;

    Button btn_signin;
    Button tv_signTip;

    LinearLayout ll_getSocreLayout;
    TextView tv_getSocreStr;
    View v_getLine;

    LinearLayout ll_outSocreLayout;
    TextView tv_outSocreStr;
    View v_outLine;

    LinearLayout ll_showCalender;

    NestedScrollView nsv_dataview;

    TextView tv_allScore;

    TextView tv_getScore;

    TextView tv_outScore;

    TextView tv_date;

    AccumPointReq accumPointReq = new AccumPointReq();
    int tradeType = 1;
    int pagenum = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
        setContentView(R.layout.activity_signin);

        recycle_list = findViewById(R.id.recycle_list);

        btn_signin = findViewById(R.id.btn_signin);
        btn_signin.setOnClickListener(this);

        tv_signTip= findViewById(R.id.tv_signTip);
        tv_signTip.setOnClickListener(this);
        ll_getSocreLayout= findViewById(R.id.ll_getSocreLayout);
        ll_getSocreLayout.setOnClickListener(this);
        tv_getSocreStr= findViewById(R.id.tv_getSocreStr);
        v_getLine = findViewById(R.id.v_getLine);

        ll_outSocreLayout= findViewById(R.id.ll_outSocreLayout);
        ll_outSocreLayout.setOnClickListener(this);
        tv_outSocreStr= findViewById(R.id.tv_outSocreStr);
        v_outLine = findViewById(R.id.v_outLine);

        ll_showCalender= findViewById(R.id.ll_showCalender);
        ll_showCalender.setOnClickListener(this);

        nsv_dataview= findViewById(R.id.nsv_dataview);

        tv_allScore= findViewById(R.id.tv_allScore);

        tv_getScore= findViewById(R.id.tv_getScore);
        tv_outScore= findViewById(R.id.tv_outScore);

        tv_date= findViewById(R.id.tv_date);

        findViewById(R.id.tv_scoreRule).setOnClickListener(this);


        initAdapter();
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0f)
                .statusBarColor(R.color.white).init();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        recycle_list.setLayoutManager(layoutManager);
        recycle_list.setHasFixedSize(false);
        recycle_list.setNestedScrollingEnabled(false);

        View empty = LayoutInflater.from(this).inflate(R.layout.layout_signin_emptydata, null, false);
        adapter.setEmptyView(empty);

        recycle_list.setAdapter(adapter);

        nsv_dataview.setVisibility(View.VISIBLE);

        btn_signin.setVisibility(View.GONE);
        tv_signTip.setVisibility(View.GONE);

    }

    @Override
    protected void onResume() {
        super.onResume();

        adapter.setNewData(new ArrayList());

        pagenum = 1;
        accumPointReq.setTradeType(tradeType);
        accumPointReq.setPage_num(pagenum);

        presenter.checkSign();
        presenter.getScore();
        presenter.getScoreList(accumPointReq);


    }

    private void initAdapter() {

        //获取的分数
        adapter = new BaseQuickAdapter<AccumPointRes.CreditInfoBean, BaseViewHolder>(R.layout.item_signin) {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            protected void convert(BaseViewHolder helper, AccumPointRes.CreditInfoBean item) {

                helper.setText(R.id.tv_point, Html.fromHtml("<font color='#00AAFF'>"
                        +item.getCredit()+"</font> <font color='#637077'>积分</font>",Html.FROM_HTML_MODE_LEGACY));
                helper.setText(R.id.tv_time,item.getGetTime());
                helper.setText(R.id.tv_title,item.getCreditTypeName());

            }
        };

        adapter.setNewData(null);

    }


    @Override
    protected BasePresenter[] initPresenters() {
        return new BasePresenter[]{ presenter };
    }

    @Override
    protected CharSequence getTopTitle() {
        return "我的积分";
    }

    public void onClick1(View v) {
        if (!ButtonUtils.isFastClick()) {
            return;
        }
        int id = v.getId();
        if (id == R.id.ll_getSocreLayout) {
            tv_getSocreStr.setTextColor(ContextCompat.getColor(this, R.color.color_00AAFF));
            v_getLine.setVisibility(View.VISIBLE);
            tv_outSocreStr.setTextColor(ContextCompat.getColor(this, R.color.color_637077));
            v_outLine.setVisibility(View.INVISIBLE);
            tradeType = 1;
            pagenum = 1;
            //请求
            adapter.setNewData(new ArrayList());
            accumPointReq.setTradeType(tradeType);
            accumPointReq.setPage_num(pagenum);
            presenter.getScoreList(accumPointReq);
        } else if (id == R.id.ll_outSocreLayout) {
            tv_getSocreStr.setTextColor(ContextCompat.getColor(this, R.color.color_637077));
            v_getLine.setVisibility(View.INVISIBLE);
            tv_outSocreStr.setTextColor(ContextCompat.getColor(this, R.color.color_00AAFF));
            v_outLine.setVisibility(View.VISIBLE);
            tradeType = 2;
            pagenum = 1;
            //请求
            adapter.setNewData(new ArrayList());
            accumPointReq.setTradeType(tradeType);
            accumPointReq.setPage_num(pagenum);
            presenter.getScoreList(accumPointReq);
        } else if (id == R.id.ll_showCalender) {
            CalendarPopWindow mPop = new CalendarPopWindow(this);
            mPop.showPopupWindow(ll_showCalender);
            mPop.setOnItemClickListener(data -> {
                if (data.length() == 0) {
                    tv_date.setText("全部");
                } else {
                    tv_date.setText(data);
                }

                accumPointReq.setDate(data);//设置当前月
                tradeType = 1;
                pagenum = 1;
                //请求
                adapter.setNewData(new ArrayList());
                accumPointReq.setTradeType(tradeType);
                accumPointReq.setPage_num(pagenum);
                presenter.getScoreList(accumPointReq);
            });
        } else if (id == R.id.tv_signTip) {
            Intent intent = new Intent(this, SignInCalendarActivity.class);
            startActivity(intent);
        } else if (id == R.id.tv_scoreRule) {
            String url = "https://newapp.szsmk.com/images/app/appCredit.html";
            Bundle bundle = new Bundle();
            bundle.putString("url", url);
            bundle.putString(Constant.TITLE, "积分规则");
            JumpUtils.jumptoNatvie(this, "webView", 0, bundle);
        } else if (id == R.id.btn_signin) {
            presenter.signIn();
        }
    }


    @Override
    public void onSignInSuccess() {
        btn_signin.setVisibility(View.GONE);
        tv_signTip.setVisibility(View.VISIBLE);
        Intent intent = new Intent(this,SignInCalendarActivity.class);
        startActivity(intent);
    }

    @Override
    public void checkSignResult(SignInRes signInRes) {
        if(signInRes.getCheckSign()==1){
            btn_signin.setVisibility(View.GONE);
            tv_signTip.setVisibility(View.VISIBLE);
            tv_signTip.setText("已连续签到"+signInRes.getKeepSignDays()+"天");
        }else{
            btn_signin.setVisibility(View.VISIBLE);
            tv_signTip.setVisibility(View.GONE);
        }
    }

    @Override
    public void getScore(SignInRes signInRes) {
        tv_allScore.setText(signInRes.getCredit()+"");

        if("0".equals(signInRes.getCreditGet())){
            tv_getScore.setText(signInRes.getCreditGet());
        }else{
            tv_getScore.setText("+"+signInRes.getCreditGet());
        }

        if("0".equals(signInRes.getCredituse())){
            tv_outScore.setText(signInRes.getCredituse());
        }else {
            tv_outScore.setText("-" + signInRes.getCredituse());
        }
    }

    @Override
    public void getScoreListResult(AccumPointRes accumPointRes) {
        ////交易类型  1 积分获取 2 积分支出
        if (tradeType == 1) {
            if (pagenum == 1) {
                adapter.replaceData(accumPointRes.getCreditInfoGenList());
            } else {
                adapter.addData(accumPointRes.getCreditInfoGenList());
            }
            adapter.loadMoreComplete();
            if (accumPointRes.getCurrentPage() == accumPointRes.getTotalPage()) {
                adapter.loadMoreEnd();
            } else {
                pagenum++;
            }
        } else {
            if (pagenum == 1) {
                adapter.replaceData(accumPointRes.getCreditInfoUseList());
            } else {
                adapter.addData(accumPointRes.getCreditInfoUseList());
            }
            adapter.loadMoreComplete();
            if (accumPointRes.getCurrentPage() == accumPointRes.getTotalPage()) {
                adapter.loadMoreEnd();
            } else {
                pagenum++;
            }
        }
    }

    @Override
    public void onFailure(String message) {

    }

    @Override
    public void onClick(View v) {
        onClick1(v);
    }
}
